package cn.remex.core.util;

import cn.remex.core.CoreSvo;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;

/**
 * 请求帮助类
 */

public class RequestHelper {
	/**
	 * 获取属性名的值,如果属性名为string，则返回string类型的值，如果为文件，则返回文件路径
	 *
	 * @param name   属性名
	 * @param params Map形式的参数集
	 * @param <T>    参数泛型
	 * @return 获取的值
	 */
	@SuppressWarnings("unchecked")
	static public <T> T getParameter(final String name, final Map<String, Object> params) {
		if (null == params || null == params.get(name)) {
			return null;
		} else {
			Object o = params.get(name);
			if (o instanceof String[]) {
				return (T) ((String[]) o)[0].toString().trim();
			} else if (o instanceof File[]) {
				return (T) ((File[]) o)[0];
			} else {
				return (T) o;
			}
		}
	}

	public static String getClientIP(HttpServletRequest request) {
		if (null == request)
			return "";
		String ip = null;

		String ips = request.getHeader("x-forwarded-for");
		if(!Judgment.nullOrBlank(ips)){
			for (String xff_ip : ips.split(",")) {
				if(!Judgment.nullOrBlank(xff_ip) && !"unknown".equalsIgnoreCase(xff_ip)){
					ip = xff_ip;
					break;
				}
			}
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-real-ip");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public static String getAppChannel(boolean forceApp){
		String appChannel = (String)CoreSvo.valLocal(CoreSvo.HTTP_HEADER_APP_CHANNEL);
		if(Judgment.notEmpty(appChannel)){
			if(forceApp && ("android".equalsIgnoreCase(appChannel) || "ios".equalsIgnoreCase(appChannel))){
				return "app";
			}
			return appChannel;
		}
		return "";
	}

	public static String getAppVersion(){
		String appVersion = (String)CoreSvo.valLocal(CoreSvo.HTTP_HEADER_APP_VERSION);
		if(Judgment.notEmpty(appVersion)){
			return appVersion;
		}
		return "";
	}
}
